<template>
  <transition name="fade" @after-leave="afterLeave">
    <div
      v-if="show"
      class="my-message"
      :class="{ ['my-message--' + type]: true }"
    >
      <i class="my-message__icon iconfont icon-info"></i>
      <p class="my-message__content">{{ message }}</p>
    </div>
  </transition>
</template>

<script>
export default {
  data () {
    return {
      type: 'info',
      message: '提示一下',
      duration: 3000,
      show: false
    }
  },
  methods: {
    showMessage () {
      this.show = true
      setTimeout(() => {
        this.show = false
      }, this.duration)
    },
    // beforeEnter () {
    //   console.log('我来了')
    // },
    afterLeave () {
      document.body.removeChild(this.$el)
    }
  }
}
</script>

<style lang="less">
.my-message {
  min-width: 380px;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  border-width: 1px;
  border-style: solid;
  border-color: #ebeef5;
  position: fixed;
  z-index: 9999;
  left: 50%;
  top: 20px;
  transform: translateX(-50%);
  background-color: #edf2fc;
  padding: 15px 15px 15px 20px;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  // 根据类型设置不同的外观
  &.my-message--info {
    color: #909399;
  }
  // success
  &.my-message--success {
    background-color: #f0f9eb;
    border-color: #e1f3d8;
    .my-message__icon {
      color: #67c23a;
    }
    .my-message__content {
      color: #67c23a;
    }
  }
  // warning
  &.my-message--warning {
    background-color: #fdf6ec;
    border-color: #faecd8;
    .my-message__icon {
      color: #e6a23c;
    }
    .my-message__content {
      color: #e6a23c;
    }
  }
  // error
  &.my-message--error {
    background-color: #fef0f0;
    border-color: #fde2e2;
    .my-message__icon {
      color: #f56c6c;
    }
    .my-message__content {
      color: #f56c6c;
    }
  }
  .my-message__icon {
    margin-right: 10px;
  }
  .my-message__content {
    padding: 0;
    font-size: 14px;
    line-height: 1;
    margin: 0;
  }
}
.fade-enter-active, // 进入/离开动画的执行体
.fade-leave-active {
  transition: all 1s;
}
.fade-enter, // 进入前状态，进入时，希望从什么状态变道正常状态 ；离开时状态，离开时，希望从正常状态变到什么状态
.fade-leave-to {
  transform: translate(-50%, -100px);
}
</style>
